package com.rgs.base.tasks.task;

import cn.hutool.core.thread.ExecutorBuilder;
import com.rgs.base.core.model.WebApiReturnResultModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * @author Ren Gaoshuai
 * @ClassName AsyncTaskThreadPool
 * @date 2022-03-08 16:07
 **/
@Data
@Slf4j
public class AsyncTaskThreadPool {

    /**
     * 创建线程池
     */
    private static final ExecutorService EXECUTOR =
            ExecutorBuilder.create()
                    .setAllowCoreThreadTimeOut(false)
                    .build();

    /**
     * 将任务加入线程池
     */
    private static Future<WebApiReturnResultModel<Void>> executeTask(AsyncTaskRunnable asyncTaskRunnable) {
        return EXECUTOR.submit(asyncTaskRunnable);
    }

    /**
     * 日志任务加入线程池
     */
    public static Future<Long> logTask(Callable<Long> task) {
        return EXECUTOR.submit(task);
    }


    public static Future<WebApiReturnResultModel<Void>> executeTask(
            AsyncTaskRunnableBO asyncTaskRunnableBO,
            AsyncTaskHandle asyncTaskHandle) {
        AsyncTaskRunnable asyncTaskRunnable = new AsyncTaskRunnable(
                asyncTaskRunnableBO,
                asyncTaskHandle
        );
        return AsyncTaskThreadPool.executeTask(asyncTaskRunnable);
    }
}
